package thread.jield;
/**
 * @author cyt
 * @create 2020-09-30 20:16
 */
public class JiedeThread implements Runnable {

    @Override
    public void run() {
        for (int i = 0; i <= 10; i++) {
            System.out.println("now run thread is : " + Thread.currentThread().getName() + " --->" + i);
            if (i == 8) {
                System.out.println("礼让的线程为 : " + Thread.currentThread().getName() + " --->" + i);
                /*
                    线程礼让时， 当前线程进入就绪状态。与它同优先级的线程重新等待CPU的调度
                    当前礼让的线程会重新获得cpu，继续执行
                    线程礼让只是提平 一个可能，并不写会真正的礼让
                */
                Thread.yield();
                try {
                    Thread.sleep(500);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

        }
    }
}
